package ZorkObjects;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class Room extends ZorkObject
{
    private ArrayList<Border> borders;
    private ArrayList<Container> containers;
    private ArrayList<Item> items;
    private ArrayList<Creature> creatures;
    private String type;

	public String getType() {
		return type;
	}

    public Room(HashMap baseConfigMap, HashMap extraConfigMap)
    {
        super(baseConfigMap);

        String[] remainingKeys = ((Set<String>)baseConfigMap.keySet())
            .toArray(new String[0]); 

        Set<String> extraKeys = (Set<String>)extraConfigMap.keySet();

        creatures = new ArrayList<Creature>();
        items = new ArrayList<Item>();
        borders = new ArrayList<Border>();
        containers = new ArrayList<Container>();

        for(String key : remainingKeys) 
        {
            if(extraKeys.contains(key)) 
            {
                Object a = baseConfigMap.remove(key);

                if(a instanceof ArrayList)
                {
                    for(String extraObjectName : (ArrayList<String>)a)
                    {
                        go(key, extraObjectName, extraConfigMap);
                    }
                }
                else if(a instanceof String)
                {
                    go(key, (String)a, extraConfigMap);
                }
                else {
                    System.out.println("this should never happen room");
                }

                String extraObjectName = (String)baseConfigMap.remove(key);
            }
            else if(key.equals("type"))
            {
                type = (String)baseConfigMap.remove(key);
            }
            else if(key.equals("border"))
            {
                Object borderContainer = baseConfigMap.remove(key);
                if(borderContainer instanceof ArrayList)
                {
                    for(HashMap h : (ArrayList<HashMap>)borderContainer)
                    {
                        borders.add(new Border(h));
                    }
                }
                else if(borderContainer instanceof HashMap)
                {
                    borders.add(new Border((HashMap)borderContainer));
                }
                else {
                    System.out.println("this should never happen border shit");
                }
            }
        }
    }

    private void go(String key, String extraObjectName, HashMap extraConfigMap) 
    {
        Object o = extraConfigMap.get(key);

        if(o instanceof ArrayList) 
        {
            ArrayList<HashMap> temp = (ArrayList<HashMap>)o;
            for(int i = 0; i < temp.size(); i++) 
            {
                HashMap h = temp.get(i);
                if(h.get("name").equals(extraObjectName)) 
                {
                    if(key.equals("container"))
                    {
                        containers.add(new Container(h, extraConfigMap));
                    }
                    else if(key.equals("item"))
                    {
                        items.add(new Item(h));
                    }
                    else
                    {
                        System.out.println("else1!!!");
                        System.out.println(key);
                        System.out.println(h.get("name"));
                    }
                }
            }
        }
        else if(o instanceof HashMap) 
        {
            creatures.add(new Creature((HashMap)o));

            /*
            if(extraObjectName.equals("gnome")) 
            {
                creatures.add(new Creature((HashMap)o));
            }
            else 
            {
                for(int i = 0; i < 100; i++) 
                {
                    System.out.println(extraObjectName);
                }                        
            }
            */
        }
        else 
        {
            System.out.println("shouldn't happen room");
        }
    }
    
    public ArrayList<Border> getBorders() { 
        return borders;
    }

	public ArrayList<Item> getItems() {
		return items;
	}

	public ArrayList<Container> getContainers() {
		return containers;
	}

	public ArrayList<Creature> getCreatures() {
		return creatures;
	}

    public String toString()
    {
        String base = super.toString();

        base += "containers =\n";
        for(Container c : containers) {
            base += c.toString() + "\n";
        }

        base += "borders =\n";
        for(Border b : borders) {
            base += b.toString() + "\n";
        }

        base += "items=\n";
        for(Item i : items) {
            base += i.toString() + "\n";
        }

        base += "creatures =\n";
        for(Creature c : creatures) { 
            base += c.toString() + "\n";
        }
        
        base += String.format("type=%s", type);

        return base;
    }
}

